package com.seek.springevent.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.seek.springevent.dao.CustomerMobileInfoDao;
import com.seek.springevent.entity.CustomerMobileInfo;
import com.seek.springevent.event.PaixuEvent;
import com.seek.springevent.service.CustomerMobileInfoService;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;

@Log
@Service("customerMobileInfoService")
public class CustomerMobileInfoServiceImpl implements CustomerMobileInfoService {

    @Autowired
    private CustomerMobileInfoDao customerMobileInfoDao;

    @Autowired
    private ApplicationContext applicationContext;


    @Autowired
    private ExecutorService executorService;

    @Transactional()
    public void processInfo(Long customerId,String phone) {
        log.info("===================processInfo调用====================");
//        Map<String,Object> params = new HashMap<>();
//        params.put("customerId",customerId);
//        params.put("filePath","https://zhaofan.b0.upaiyun.com/server/other/20181114/9c9f39243d86dce82ef83ee17497cc3e.json");
//        customerMobileInfoDao.updateInfoById(params);
        log.info("===================更新filePath====================");
        synchronized (phone) {
            CustomerMobileInfo customerMobileInfo = customerMobileInfoDao.selectList(new EntityWrapper<CustomerMobileInfo>().eq("customer_id", customerId)).get(0);
            customerMobileInfo.setFilePath("https://zhaofan.b0.upaiyun.com/server/other/20181114/9c9f39243d86dce82ef83ee17497cc3e.json");
            customerMobileInfo.setLastModifyTime(new Date());
            customerMobileInfoDao.updateAllColumnById(customerMobileInfo);
            log.info("fileInfo" + JSONObject.toJSONString(customerMobileInfo));
            if (StringUtils.isNotBlank(customerMobileInfo.getReportFile()) && StringUtils.isNotBlank(customerMobileInfo.getFilePath())) {
                log.info("=================process info中调用排序分====================");
            }
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }


    @Transactional
    public void processReport(Long customerId,String phone) {
        log.info("===================processReport调用====================");
//        Map<String,Object> params = new HashMap<>();
//        params.put("customerId",customerId);
//        params.put("reportFile","https://zhaofan.b0.upaiyun.com/server/other/20181114/e0a39a475b9a92a2a94ee3102f831eda.json");
//        customerMobileInfoDao.updateReportById(params);
        synchronized (phone) {
            CustomerMobileInfo customerMobileInfo = customerMobileInfoDao.selectList(new EntityWrapper<CustomerMobileInfo>().eq("customer_id", customerId)).get(0);
            customerMobileInfo.setReportFile("https://zhaofan.b0.upaiyun.com/server/other/20181114/e0a39a475b9a92a2a94ee3102f831eda.json");
            customerMobileInfo.setLastModifyTime(new Date());
            log.info("===================更新reportPath====================");
            customerMobileInfoDao.updateAllColumnById(customerMobileInfo);
            log.info("report更新后的信息:" + JSONObject.toJSONString(customerMobileInfo));
            if (StringUtils.isNotBlank(customerMobileInfo.getReportFile()) && StringUtils.isNotBlank(customerMobileInfo.getFilePath())) {
                log.info("=================process report中调用排序分==================");
            }
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    @Transactional
    public void processInfoAndPublishEvent(Long customerId) {
        log.info("===================processInfo调用====================");
        Map<String,Object> params = new HashMap<>();
        params.put("customerId",customerId);
        params.put("filePath","https://zhaofan.b0.upaiyun.com/server/other/20181114/9c9f39243d86dce82ef83ee17497cc3e.json");
        customerMobileInfoDao.updateInfoById(params);
        log.info("===================更新filePath====================");
        CustomerMobileInfo customerMobileInfo = customerMobileInfoDao.selectList(new EntityWrapper<CustomerMobileInfo>().eq("customer_id",customerId)).get(0);
        log.info("fileInfo"+JSONObject.toJSONString(customerMobileInfo));
        if (StringUtils.isNotBlank(customerMobileInfo.getReportFile())&&StringUtils.isNotBlank(customerMobileInfo.getFilePath())){
            log.info("=================process info中调用排序分====================");
        }
        applicationContext.publishEvent(new PaixuEvent(this,customerId));
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
